﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

namespace AutoCAD_CSharp_plug_in3.Chap04
{
    public static class Block2
    {
        private static readonly Dictionary<string, double[]> Dn = new Dictionary<string, double[]>
        {
            // 格式: A, d1(1), d1(2), d2(1), d2(2)  
            { "DN15", new double[] { 38, 27, 27, 49, 49 } },
            { "DN20", new[] { 38, 24.5, 24.5, 51.5, 51.5 } },
            { "DN25", new double[] { 38, 21, 21, 55, 55 } },
            { "DN32", new double[] { 48, 27, 27, 69, 69 } },
            { "DN40", new double[] { 57, 33, 33, 81, 81 } },
            { "DN50", new double[] { 76, 46, 46, 106, 106 } },
            { "DN65", new double[] { 95, 57, 57, 133, 133 } },
            { "DN80", new[] { 114, 69.5, 69.5, 158.5, 158.5 } },
            { "DN100", new double[] { 152, 95, 95, 209, 209 } },
            { "DN125", new double[] { 190, 120, 120, 260, 260 } },
            { "DN150", new double[] { 229, 145, 145, 313, 313 } }
        };

        private static double[] GetDnVal(string key)
        {
            return Dn.TryGetValue(key, out var val) ? val : Dn.Values.First();
        }

        /// <summary>
        ///     创建 弯管系列-主视 块
        /// </summary>
        /// <param name="blockName">定义块名称</param>
        /// <param name="dn">型号</param>
        /// <returns>块定义</returns>
        private static BlockTableRecord Create(out string blockName, string dn)
        {
            blockName = "弯管系列-主视" + "_" + dn;
            var blockDef = new BlockTableRecord
            {
                Name = blockName,
                Origin = Point3d.Origin
            };

            var dnVal = GetDnVal(dn);
            var a = dnVal[0];
            var d11 = dnVal[1];
            var d12 = dnVal[2];
            var d21 = dnVal[3];
            var d22 = dnVal[4];

            const double startAngle = 90;
            const double endAngle = 180;
            const double startRad = startAngle * Math.PI / 180.0;
            const double endRad = endAngle * Math.PI / 180.0;

            var po = Point3d.Origin;

            var topArc = CreateArc(po, d21, startRad, endRad, 256);
            var middleArc = CreateArc(po, a, startRad, endRad, 4);
            var bottomArc = CreateArc(po, d11, startRad, endRad, 256);

            blockDef.AppendEntity(topArc);
            blockDef.AppendEntity(middleArc);
            blockDef.AppendEntity(bottomArc);

            var t1 = new Point3d(
                po.X + d22 * Math.Cos(Math.PI / 2),
                po.Y + d22 * Math.Sin(Math.PI / 2),
                0
            );

            var t2 = new Point3d(
                po.X + d12 * Math.Cos(Math.PI / 2),
                po.Y + d12 * Math.Sin(Math.PI / 2),
                0
            );

            var t3 = new Point3d(
                po.X + d21 * Math.Cos(Math.PI),
                po.Y + d21 * Math.Sin(Math.PI),
                0
            );


            var t4 = new Point3d(
                po.X + d11 * Math.Cos(Math.PI),
                po.Y + d11 * Math.Sin(Math.PI),
                0
            );

            var l1 = new Line(t1, t2)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };

            var l2 = new Line(t3, t4)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };

            blockDef.AppendEntity(l1);
            blockDef.AppendEntity(l2);
            return blockDef;
        }

        private static Arc CreateArc(Point3d center, double radius, double startAngle, double endAngle,
            short colorIndex)
        {
            return new Arc(center, radius, startAngle, endAngle)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, colorIndex)
            };
        }

        public static List<BlockTableRecord> CreateAll(string blockNamePrefix)
        {
            var blocks = new List<BlockTableRecord>();
            foreach (var pair in Dn)
            {
                var record = Create(out var blockName, pair.Key);
                record.Name = blockNamePrefix + blockName;

                blocks.Add(record);
            }

            return blocks;
        }
    }
}